package com.unittec.drainage.provider.auth.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.unittec.drainage.provider.auth.entity.SysLimitFunction;
import com.unittec.drainage.provider.auth.mapper.LimitFunctionMapper;
import com.unittec.drainage.provider.root.ServiceRoot;
import com.unittec.drainage.provider.service.HystrixRestTemplateService;
import com.unittec.drainage.sdk.root.common.FieldConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: tangsicong
 * @date: 2019-6-25
 */
@Component
public class LimitFunctionService extends ServiceRoot<LimitFunctionMapper, SysLimitFunction> {
    @Autowired
    private HystrixRestTemplateService hystrixRestTemplateService;

    @Cacheable(value = "xxzyt-auth-100",
        key = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId+':LimitFunc'+ #functionId",
        condition = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId != null")
    public SysLimitFunction getOneLimitFunction(String functionId, String supplierSettingId) {

        return getOne(new LambdaQueryWrapper<SysLimitFunction>().eq(SysLimitFunction::getFunctionCode, functionId)
            .or(i -> i.eq(SysLimitFunction::getAppId, FieldConstants.SYS_APP_ID)
                .eq(SysLimitFunction::getTenantId, FieldConstants.SYS_TENANT_ID)
                .eq(SysLimitFunction::getFunctionCode, functionId)));
    }

    // @Cacheable(value = "xxzyt-auth-100", key="T(ThreadLocalScope).appId+':LimitFunction'+ #functionId",
    // condition = "T(ThreadLocalScope).appId != null")
    public SysLimitFunction getLimitFunction(String functionId) {
        return getOne(new LambdaQueryWrapper<SysLimitFunction>().eq(SysLimitFunction::getFunctionCode, functionId));
    }

    private List<SysLimitFunction> getListLimitFunction(String functionId) {
        return list(new LambdaQueryWrapper<SysLimitFunction>().eq(SysLimitFunction::getFunctionCode, functionId));

    }

    private boolean saveLimitFunction(SysLimitFunction limitFunction) {
        return save(limitFunction);
    }

    @CacheEvict(value = "xxzyt-auth-100",
        key = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId+':LimitFunc'+ #limitFunction.getFunctionCode()",
        condition = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId != null")
    public boolean updateLimitFunction(SysLimitFunction limitFunction) {
        return update(limitFunction, new LambdaQueryWrapper<SysLimitFunction>().eq(SysLimitFunction::getFunctionCode,
            limitFunction.getFunctionCode()));
    }

    @CacheEvict(value = "xxzyt-auth-100",
        key = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId+':LimitFunc'+ #limitFunction.getFunctionCode()",
        condition = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId != null")
    public boolean judgeLimitFunction(SysLimitFunction limitFunction) {
        SysLimitFunction function = getLimitFunction(limitFunction.getFunctionCode());
        if (function == null) {
            return saveLimitFunction(limitFunction);
        } else {
            return updateLimitFunction(limitFunction);
        }
    }

    @CacheEvict(value = "xxzyt-auth-100",
        key = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId+':LimitFunc'+ #functionCode",
        condition = "T(com.unittec.drainage.provider.scope.ThreadLocalScope).appId != null")
    public boolean deleteLimitFunction(String functionCode) {
        return remove(new LambdaQueryWrapper<SysLimitFunction>().eq(SysLimitFunction::getFunctionCode, functionCode));
    }
}
